﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Raven.Client;
using Raven.Client.Document;

namespace kinokasse.belegungenrepo
{
    public class DocumentStoreFactory
    {
        public const string ConnectionStringName = @"BelegungenRepo";
        public const string DefaultDataDirectory = @".\DB";


        public static IDocumentStore InitializeDocumentStore()
        {
            IDocumentStore documentStore = null;
           
            var connectionsStringSetting = ConfigurationManager.ConnectionStrings[ConnectionStringName];
            if (connectionsStringSetting == null)
            {
                documentStore = new DocumentStore { DataDirectory = DefaultDataDirectory };
            }
            else
            {
                System.Uri resultUri;
                if (System.Uri.TryCreate(connectionsStringSetting.ConnectionString, UriKind.Absolute, out resultUri) &&
                    resultUri.IsAbsoluteUri && !resultUri.IsFile)
                {
                    documentStore = new DocumentStore { Url = resultUri.AbsoluteUri };
                }
                else
                {
                    documentStore = new DocumentStore { DataDirectory = connectionsStringSetting.ConnectionString };
                }
            }

            return documentStore.Initialize();
        }
    }
}
